//自定义网页编辑插件
;(function($){
	var deParams = {};
	var deBoxs = {};
	var defaultParams = {
			lboxWidth : 0.02//设定拖动小格子一格为宽度的百分之1
	};
	
    $.fn.webeditor=function(params,data){
    	params.stage = $(this);
    	//匹配内置方法
    	if("addOne" == params){
    		var uuid = $(this).data("uuid");
    		var maxTop = 0;
    		for(var key in deBoxs){
    			var boxTop = $(deBoxs[key]).position().top;
    			var boxHeight = $(deBoxs[key]).height();
    			if(maxTop < (boxTop+boxHeight)){
    				maxTop = boxTop+boxHeight;
    			}
    		}
    		data.isNew = true;
    		//偏移5点距离
    		data.top =  0 == maxTop ? 0 : (maxTop + 5);
    		buildOneBox(deParams[uuid],data,true);
    	}else if("deleteOne" == params){
    		delete deBoxs[data];
    	}else if("getBox" == params){
    		return deBoxs[data];
    	}else if("setBoxProps" == params){
    		var box = deBoxs[data.uuid];
    		data.uuid = box.data("uuid");
    		box.deBox("setProps",data);
    	}else if("saveAll" == params){
    		var uuid = $(this).data("uuid");
    		saveAll(deParams[uuid],data.success);
    	}else{
    		params.stage.html("");
    		//初始化页面
    		initWebPage(params,renderWebPage);
    	}
    }
    
    //初始化页面
    function initWebPage(params,callback){
    	var uuid = CreateUuid();
    	params.stage.data("uuid",uuid);
		deParams[uuid] = params;
    	$.ajax({
    		url : params.url,
    		type : "post",
    		data : params.data,
    		success : function(data){
    			callback(params,data);
    		}
    	});
    }
    
    //渲染编辑的Web页面
    function renderWebPage(params,data){
		for(var i = 0; i < data.length; i++){
    		buildOneBox(params,data[i]);
    	}
    	//添加网格线
    	var totalWidth = params.stage.width();
    	if("phone" == params.mode){
    		 defaultParams.lboxWidth = 0.04;
    	}
    	var pwgWidth = defaultParams.lboxWidth * totalWidth;
    	var wgxnum = 1/defaultParams.lboxWidth;
    	var left = 0;
    	//最后一条不需要画
    	for(var i = 0; i < (wgxnum-1); i++){
    		left += pwgWidth;
    		params.stage.append("<div class='wgx-x' style='left:"+left+"px;'></div>");
    	}
    }
    
    //在stage放置一个盒子
    function buildOneBox(params,info){
    	info.opts = JSON.parse(info.opts);
    	var box = $("<div class='pageElements biggerboxs'></div>").deBox(params,info);
    	deBoxs[box.deBox("getUuid")] = box;
    	params.stage.append(box);
    	if("1" == box.data("sfmrdj")){
    		box.click();
    	}
    }
    
    //保存
    function saveAll(params,callback){
    	var jsonStr = "";
    	var stageWidth = params.stage.width();
    	var pageId = params.data.pageId;
    	var cons = new Array();
    	$.each(deBoxs,function(key,value){
    		var obj = value;
    		var con = obj.deBox("getInfo",{stageWidth:stageWidth,pageId:pageId});
    		cons.push(con);
    	});
    	jsonStr = JSON.stringify(cons);
    	$.ajax({
    		url : params.saveUrl,
    		type : "post",
    		data : {
    			jsonStr : jsonStr,
    			pageId : pageId
			},
    		success : function(data){
    			if(data.success){
    				//showMsg("保存成功！");
    				callback(data);
    			}else{
    				//showMsg(data.returnMsg);
    			}
    		}
    	});
    }
    
    function CreateUuid(){
    	var mydate = new Date();
    	var rndNum = RndNum(6);
    	return "stages"+mydate.getDay()+ mydate.getHours()+ mydate.getMinutes()+mydate.getSeconds()+mydate.getMilliseconds()+rndNum;
    }
    
    function RndNum(n){
        var rnd="";
        for(var i=0;i<n;i++)
            rnd+=Math.floor(Math.random()*10);
        return rnd;
    }
})(jQuery); 